package com.jfl.base;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.ui.ModelMap;

import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;
import com.jfl.common.ReturnMessageKey;
import com.jfl.enums.HttpCode;
import com.jfl.utils.FastJsonUtil;

/**
 * 抽象控制器
 * 
 * @author 赵炎
 * @version [V1.00, 2018年11月29日]
 * @since V1.00
 */
public abstract class AbstractController
{
    /**
     * logger：系统日志
     */
    protected final Logger logger = LoggerFactory.getLogger(getClass());
    
    /**
     * <一句话功能简述>
     * 
     * @return
     * @see [类、类#方法、类#成员]
     */
    protected ResponseEntity<ModelMap> setSuccessModelMap()
    {
        return setSuccessModelMap(new ModelMap(), null);
    }
    
    
    /**
     * <一句话功能简述>
     * 
     * @param modelMap
     * @return
     * @see [类、类#方法、类#成员]
     */
    protected ResponseEntity<ModelMap> setSuccessModelMap(ModelMap modelMap)
    {
        return setSuccessModelMap(modelMap, null);
    }
    
    /**
     * <一句话功能简述>
     * 
     * @param data
     * @return
     * @see [类、类#方法、类#成员]
     */
    protected ResponseEntity<ModelMap> setSuccessModelMap(Object data)
    {
        return setModelMap(new ModelMap(), HttpCode.OK, data);
    }
    
    /**
     * <一句话功能简述>
     * 
     * @param modelMap
     * @param data
     * @return
     * @see [类、类#方法、类#成员]
     */
    protected ResponseEntity<ModelMap> setSuccessModelMap(ModelMap modelMap, Object data)
    {
        return setModelMap(modelMap, HttpCode.OK, data);
    }
    
    /**
     * <一句话功能简述>
     * 
     * @param code
     * @return
     * @see [类、类#方法、类#成员]
     */
    protected ResponseEntity<ModelMap> setModelMap(HttpCode code)
    {
        return setModelMap(new ModelMap(), code, null);
    }
    
    protected ResponseEntity<ModelMap> setModelMap(String code, String msg)
    {
        return setModelMap(new ModelMap(), code, msg, null);
    }
    
    /**
     * <一句话功能简述>
     * 
     * @param modelMap
     * @param code
     * @return
     * @see [类、类#方法、类#成员]
     */
    protected ResponseEntity<ModelMap> setModelMap(ModelMap modelMap, HttpCode code)
    {
        return setModelMap(modelMap, code, null);
    }
    
    /**
     * <一句话功能简述>
     * 
     * @param code
     * @param data
     * @return
     * @see [类、类#方法、类#成员]
     */
    protected ResponseEntity<ModelMap> setModelMap(HttpCode code, Object data)
    {
        return setModelMap(new ModelMap(), code, data);
    }
    
    /**
     * <一句话功能简述>
     * 
     * @param code
     * @param msg
     * @param data
     * @return
     * @see [类、类#方法、类#成员]
     */
    protected ResponseEntity<ModelMap> setModelMap(String code, String msg, Object data)
    {
        return setModelMap(new ModelMap(), code, msg, data);
    }
    
    /**
     * <一句话功能简述>
     * 
     * @param modelMap
     * @param code
     * @param data
     * @return
     * @see [类、类#方法、类#成员]
     */
    protected ResponseEntity<ModelMap> setModelMap(ModelMap modelMap, HttpCode code, Object data)
    {
        return setModelMap(modelMap, code.value().toString(), code.msg(), data);
    }
    
    /**
     * <一句话功能简述>
     * 
     * @param modelMap
     * @param code
     * @param msg
     * @param data
     * @return
     * @see [类、类#方法、类#成员]
     */
    @SuppressWarnings("rawtypes")
    protected ResponseEntity<ModelMap> setModelMap(ModelMap modelMap, String code, String msg, Object data)
    {
        Map<String, Object> map;
        if (!modelMap.isEmpty())
        {
            map = Maps.newHashMap();
            map.putAll(modelMap);
            modelMap.clear();
            for (String key : map.keySet())
            {
                if ((!key.startsWith("org.springframework.validation.BindingResult")) && (!key.equals("void")))
                {
                    modelMap.put(key, map.get(key));
                }
            }
        }
        if (data != null)
        {
            if ((data instanceof PageInfo<?>))
            {
                PageInfo<?> page = (PageInfo)data;
                modelMap.put("rows", page.getList()); // 返回数据列表
                modelMap.put("pageNum", page.getPageNum());// 当前页
                modelMap.put("pageSize", page.getPageSize());// 页码大小
                modelMap.put("pages", page.getPages()); // 总页数
                modelMap.put("total", page.getTotal()); // 总条数
            }
            else if ((data instanceof List))
            {
                modelMap.put("rows", data);
                modelMap.put("total", ((List)data).size());
            }
            else if (data instanceof BaseModel)
            {
                modelMap.put(ReturnMessageKey.DATA, data);
            }
            else
            {
                modelMap.put(ReturnMessageKey.DATA, data);
            }
        }
        modelMap.put("success", Boolean.TRUE);
        modelMap.put(ReturnMessageKey.CODE, code);
        modelMap.put(ReturnMessageKey.MESSAGE, msg);
        modelMap.put(ReturnMessageKey.TIMESTAMP, Long.valueOf(System.currentTimeMillis()));
        this.logger.info("response ==>{}", FastJsonUtil.toJSONString(modelMap));
        return ResponseEntity.ok(modelMap);
    }
    
}
